<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	class lib_user_extended {
		
		// TODO: Implement these functions
		function ignore_user($uid){
			$user = new lib_user($uid);
			if($user->is_valid() && !isset($this->data_cache['ignore'][$user->data_cache['uid']])){
				$this->data_cache['ignore'][$user->data_cache['uid']] = $user->data_cache['name'];
				$this->commit_changes(array('ignore'=>$this->data_cache['ignore']));
				return $user;
			}

			return false;
		}

		function unignore_user($uid){
			$user = new lib_user($uid);
			if($user->is_valid() && isset($this->data_cache['ignore'][$user->data_cache['uid']])){
				unset($this->data_cache['ignore'][$user->data_cache['uid']]);
				$this->commit_changes(array('ignore'=>$this->data_cache['ignore']));
				return $user;
			}

			return false;
		}

		function add_buddy($uid,$override=false){
			if($override || $this->has_permission('USE_BUDDY_LIST')){
				$user = new lib_user($uid);
				if($user->is_valid() && !isset($this->data_cache['buddies'][$user->data_cache['uid']])){
					$this->data_cache['buddies'][$user->data_cache['uid']] = $user->data_cache['name'];
					$this->commit_changes(array('buddies'=>$this->data_cache['buddies']));
					// TODO: Does this work correctly?  Wouldn't it show them as online even if they are not?
					$GLOBALS['CACHE']->write("private_messages/{$this->data_cache['uid']}",array('lastUpdateTime'=>microtime_float(),'operation'=>'online_add','data'=>array('uid'=>$user->data_cache['uid'],'name'=>$user->data_cache['name'],'rid'=>'buddylist')));
					return $user;
				}
			}

			return false;
		}

		function remove_buddy($uid,$override=false){
			if($override || $this->has_permission('USE_BUDDY_LIST')){
				$user = new lib_user($uid);
				if($user->is_valid() && isset($this->data_cache['buddies'][$user->data_cache['uid']])){
					unset($this->data_cache['buddies'][$user->data_cache['uid']]);
					$this->commit_changes(array('buddies'=>$this->data_cache['buddies']));
					$GLOBALS['CACHE']->write("private_messages/{$this->data_cache['uid']}",array('lastUpdateTime'=>microtime_float(),'operation'=>'online_remove','data'=>array('uid'=>$user->data_cache['uid'],'rid'=>'buddylist')));
					return $user;
				}
			}

			return false;
		}

		/**
		 * Sends out a welcome E-Mail to the user, this E-Mail contains their username
		 * and an activation link (if one exists)
		 *
		 * @param lib_user $user
		 */
		public static function send_welcome_email($user){
			global $settings;
			
			if(empty($user->data_cache['activation_code'])){
				$msg = $GLOBALS['LANG']->lang_email->welcome_email_body;
				$url = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"];
			}elseif(strlen($user->data_cache['activation_code']) < 40){
				$msg = $GLOBALS['LANG']->lang_email->welcome_email_adminactivation_body;
				$url = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"];
			}else{
				$msg = $GLOBALS['LANG']->lang_email->welcome_email_useractivation_body;
				$url = 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"]."?act=verify&uid={$user->data_cache['uid']}&code={$user->data_cache['activation_code']}";
			}
			
			$msg = $GLOBALS['LANG']->format($msg,array('sitename',$settings['site_name']),array('url',$url),array('username',$user->data_cache['name']));
			$subject = $GLOBALS['LANG']->format($GLOBALS['LANG']->lang_email->welcome_email_subject,array('sitename',$settings['site_name']));
		
			$email = new lib_email($user->data_cache['email'],$subject,$msg);
			return $email->send();
		}
		
		public static function validate_username($name){
			global $settings;
			
			$len = x7utf8::strlen($name);
			
			if($len < $settings['name_min_length'])
				return $GLOBALS['LANG']->lang_intro->too_short_username;
			if($len > $settings['name_max_length'])
				return $GLOBALS['LANG']->lang_intro->too_long_username;
			if(preg_match("#^[0-9]*$#u",$name))
				return $GLOBALS['LANG']->lang_intro->username_is_all_numbers;
			
			// Check if this username is in use or not
			$user = new lib_user($name,'name');
			if($user->is_valid())
				return $GLOBALS['LANG']->lang_intro->username_taken_username;
			
			return '';
		}
		
		public static function validate_password($pass){
			global $settings;
			
			$len = x7utf8::strlen($pass);
		
			if($len < $settings['password_min_length'])
				return $GLOBALS['LANG']->lang_intro->too_short_user_password;
			if($len > $settings['password_max_length'])
				return $GLOBALS['LANG']->lang_intro->too_long_user_password;
				
			return '';
		}
		
		public static function validate_email($email){
			global $settings;
			
			$email = x7utf8::strtolower($email);
			
			if(!preg_match('/^[^@]*@[^.]*\..*$/u',$email))
				return $GLOBALS['LANG']->lang_intro->email_invalid_email;
				
			$len = x7utf8::strlen($email);
			if($len > $settings['max_email_length'])
				return $GLOBALS['LANG']->lang_intro->too_long_email;
			
			$user = new lib_user($email,'email');
			if($user->is_valid())
				return $GLOBALS['LANG']->lang_intro->email_taken_email;
			
			return '';
		}
		
	}

?>